//
//  DAOBasic.m
//  XMDLearn
//
//  Created by apple on 16/4/12.
//  Copyright © 2016年 hao. All rights reserved.
//

#import "DAOBasic.h"
#import "MessageDto.h"
#import "WGJsonParser.h"
#import <AFHTTPRequestOperationManager.h>

@implementation DAOBasic

-(void)post:(NSString *)url parameters:(NSDictionary *)parameters dataModel:(Class)dataModel successHandler:(void (^)(id model))successBlock errorHandler:(void (^)(NSString *code, NSString *message))errorBlock{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    manager.requestSerializer = [AFHTTPRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager POST:url parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        MessageDto *result = [WGJsonParser parseData:responseObject usingModel:[MessageDto class]];
        
        NSLog(@"怎么为空啊message:%@",responseObject);
        
        NSUserDefaults *userdefaults = [NSUserDefaults standardUserDefaults];
        [userdefaults setObject:result.code forKey:@"resultCode"];
        [userdefaults synchronize];
        
        NSArray *arr = [result getContent];
        NSLog(@"数组%ld",(unsigned long)[arr count]);
        
        if (![result.content isEqualToString:@"[]"] && ![result.content isEqualToString:@""]) {
            
            id vo = [WGJsonParser parseJson:result.content usingModel:[dataModel class]];
            successBlock(vo);
            
        } else {
            errorBlock(result.code,result.message);
        }
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error:%@",error);
    }];
    
}

@end
